package com.ycw.servlet.guoguo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ycw.dao.HouseRepairDao;
import com.ycw.dao.impl.HouseRepairDaoImpl;

/**
 * Servlet implementation class AlterHouseRepairById
 */
@WebServlet("/alterHouseRepairByIdServlet")
public class AlterHouseRepairByIdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HouseRepairDao hrd = HouseRepairDaoImpl.getInstance();
		/**
		 * 根据工号查询l_id是否存在
		 */
		String l_name = request.getParameter("l_name");

		/**
		 * 得到l_id的数据 如果l_id为0，则工号错误且notFoundl_id = true
		 */
		String h_region = request.getParameter("h_region");
		String h_village = request.getParameter("h_village");
		String h_unit = request.getParameter("h_unit");
		String h_roomnum = request.getParameter("h_roomnum");
		String l_ids = request.getParameter("l_id");
		Integer l_id = Integer.parseInt(l_ids);
		/**
		 * 得到h_id的数据 如果h_id为0，则房屋地址错误且notFoundh_id = true
		 */
		int h_id = hrd.queryH_idByAddData(h_region, h_village, h_unit, h_roomnum);
		boolean notFoundh_id = false;
		if (h_id == 0) {
			notFoundh_id = true;
		}

		boolean repeatAdd = false;
		boolean alterHouseRepairById = false;
		if (h_id != 0) {
			/**
			 * 表的其他的信息
			 */
			// 修缮时间
			String hr_time = request.getParameter("hr_time");
			// 修缮状态 已修缮，未修缮
			String hr_state = request.getParameter("hr_state");
			// 修缮说明
			String hr_explain = request.getParameter("hr_explain");
			/**
			 * 根据id修改
			 */
			String id = request.getParameter("hr_id");
			Integer hr_id = Integer.parseInt(id);
			List<Object> list = new ArrayList<>();
			list.add(h_id);
			list.add(l_id);
			list.add(hr_time);
			list.add(hr_state);
			list.add(hr_explain);
			list.add(hr_id);
			/**
			 * 修改表信息，添加成功为true
			 */
			alterHouseRepairById = hrd.alterHouseRepairById(list);
			// }
		}
		Map<String, Object> map = new HashMap<>();

		/**
		 * notFoundl_id = true表示工号不存在 notFoundh_id = true表示房子不存在 repeatAdd =
		 * true表示数据房子和维修人员的关系以及存在，不可重复 以上条件都为false时才可以修改
		 * alterHouseRepairById=true表示修改成功
		 */
		map.put("notFoundh_id", notFoundh_id);
		map.put("alterHouseRepairById", alterHouseRepairById);

		String resJSON = JSON.toJSONString(map);
		PrintWriter out = response.getWriter();
		out.write(resJSON);
	}

}
